package com.xoa.service.crm;

import com.xoa.dao.crm.CrmManagerMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.crm.CrmManager;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * Created by qiyang on 2019/7/22.
 */
@Service
public class CrmManagerService {

    @Autowired
    private CrmManagerMapper crmManagerMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 设置权限
     */
    public int setUpCrmManager(CrmManager crmManager){
        return crmManagerMapper.insert(crmManager);
    }

    /**
     * 显示设置过权限的数据
     */
    public List<CrmManager> selectByAll(Integer page, Integer pageSize, Boolean useFlag, Map map) {
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page", pageParams);
        List<CrmManager> crmManagers = crmManagerMapper.selectAll(map);
        for (CrmManager manager:crmManagers) {

            //主管
            String managerId = manager.getManagerId();

            StringBuffer stringBufferP = new StringBuffer();
            if(managerId.contains(",")){
                String[] split = managerId.split(",");
                for (int i=0;i<split.length;i++){
                    String username = usersMapper.getUsernameByUserId(split[i]);
                    //判断是否为最后一个
                    if(i == split.length-1){
                        stringBufferP.append(username);
                    }else {
                        stringBufferP.append(username+",");
                    }

                }

                manager.setManagerUserName(String.valueOf(stringBufferP));

            }


            //下属
            String salesId = manager.getSalesId();
            StringBuffer stringBuffer = new StringBuffer();
            if(salesId.contains(",")){
                String[] split = salesId.split(",");
                for (int i=0;i<split.length;i++){
                    String username = usersMapper.getUsernameByUserId(split[i]);
                    //判断是否为最后一个
                    if(i == split.length-1){
                        stringBuffer.append(username);
                    }else {
                        stringBuffer.append(username+",");
                    }

                }

                manager.setSalesUserName(String.valueOf(stringBuffer));

            }

        }
        return crmManagers;
    }

    /**
     * 删除权限
     */
    public Integer delSetUpCrmManager(Integer mid){
        return crmManagerMapper.deleteByPrimaryKey(mid);
    }

    /**
     * 编辑回显权限
     */
    public CrmManager detailSetUpCrmManager(Integer mid){
        CrmManager crmManager = crmManagerMapper.selectByPrimaryKey(mid);

        //主管
        String managerId = crmManager.getManagerId();

        StringBuffer stringBufferParent = new StringBuffer();
        if(managerId.contains(",")){
            String[] split = managerId.split(",");
            for (int i=0;i<split.length;i++){
                String usernameP = usersMapper.getUsernameByUserId(split[i]);
                //判断是否为最后一个
                if(i == split.length-1){
                    stringBufferParent.append(usernameP);
                }else {
                    stringBufferParent.append(usernameP+",");
                }

            }

            crmManager.setManagerUserName(String.valueOf(stringBufferParent));

        }

        //下属
        String salesId = crmManager.getSalesId();
        StringBuffer stringBuffer = new StringBuffer();
        if(salesId.contains(",")){
            String[] split = salesId.split(",");
            for (int i=0;i<split.length;i++){
                String username = usersMapper.getUsernameByUserId(split[i]);
                //判断是否为最后一个
                if(i == split.length-1){
                    stringBuffer.append(username);
                }else {
                    stringBuffer.append(username+",");
                }

            }

            crmManager.setSalesUserName(String.valueOf(stringBuffer));

        }

        return crmManager;
    }

    public Integer upSetUpCrmManager(CrmManager crmManager){
        return crmManagerMapper.updateByPrimaryKeySelective(crmManager);
    }

}
